﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RBAC.Web.Utilities
{
    /*
     * Author       : Soravit Pornpaiboonstid
     * Detail       : Custom DateTime model binder for Thai DateTime format
     * Date         : 29/08/2556
     * Last Update  : 29/08/2556
     * Version      : 1
     */
    public class ThaiDateTimeModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
            var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

            if (!string.IsNullOrEmpty(displayFormat) && value != null)
            {
                DateTime date;
                displayFormat = displayFormat.Replace("{0:", string.Empty).Replace("}", string.Empty);
                // use the format specified in the DisplayFormat attribute to parse the date
                if (DateTime.TryParseExact(value.AttemptedValue, displayFormat, CultureInfo.GetCultureInfo("th-TH"), DateTimeStyles.None, out date))
                {
                    return date;
                }
                /*else
                {
                    bindingContext.ModelState.AddModelError(
                        bindingContext.ModelName,
                        string.Format("{0} is an invalid date format", value.AttemptedValue)
                    );
                }*/
            }

            return base.BindModel(controllerContext, bindingContext);
        }
    }
}